<template>
    <div>
        <div>
            <el-input
                    class="addPosIntput"
                    size="small"
                    style=""
                    placeholder="请输入职位"
                    @keydown.enter.native="addPosition"
                    prefix-icon="el-icon-plus"
                    v-model="pos.name">
            </el-input>
            <el-button icon="el-icon-plus" style="" size="small" type="primary" @click="addPosition">添加</el-button>
            <el-button size="small" type="danger" style="margin-top: 8px;" :disabled="multipleSelection.length == 0" @click="deleteAll">批量删除</el-button>
        </div>
        <div class="posManaTable">

            <el-table
                    :data="positions"
                    border
                    stripe
                    size="small"
                    @selection-change="handleSelectionChange"
                    style="width: 70%">
                <el-table-column
                        type="selection"
                        width="55">
                </el-table-column>
                <el-table-column
                        prop="id"
                        label="编号"
                        width="55">
                </el-table-column>
                <el-table-column
                        prop="name"
                        label="职位名称">
                </el-table-column>
                <el-table-column
                        prop="createDate"
                        label="创建时间">
                </el-table-column>
                <el-table-column
                        label="是否启用">
                    <template slot-scope="scope">
                        <el-tag size="small" type="success" v-if="scope.row.enabled">启用</el-tag>
                        <el-tag size="small" type="danger" v-else>未启用</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="180">
                    <template slot-scope="scope">
                        <el-button
                                size="mini"
                                @click="showEditView(scope.$index, scope.row)">编辑</el-button>
                        <el-button
                                size="mini"
                                type="danger"
                                @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <el-dialog
                title="修改职位"
                :visible.sync="dialogVisible"
                width="20%">
            <div>
                <el-tag>职位名称</el-tag>
                <el-input class="updatePosInput" size="small" v-model="updatePos.name"></el-input>
                <el-tag>是否启用</el-tag>
                <el-switch
                        v-model="updatePos.enabled"
                        active-text="启用"
                        inactive-text="禁用">
                </el-switch>
            </div>
            <span slot="footer" class="dialog-footer">
            <el-button size="small" @click="dialogVisible = false">取 消</el-button>
            <el-button size="small" type="primary" @click="doUpdate">确 定</el-button>
          </span>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        name: "PosMana",
        data() {
            return {
                pos: {
                    name:''
                },
                dialogVisible:false,
                updatePos:{
                    name:'',
                    enabled:false
                },
                positions: [],
                multipleSelection: []
            }
        },
        mounted() {
            this.innitPositions();
        },
        methods:{
            //批量删除：点击回调函数,选中多选框当前行赋值
            handleSelectionChange(val) {
                this.multipleSelection = val;
                console.log(val);
            },
            deleteAll() {

                this.$confirm('此操作将永久删除该【'+this.multipleSelection.length+'】条, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    let ids = '?';
                    this.multipleSelection.forEach(item => {
                        ids += 'ids='+item.id +'&';
                    });
                    this.deleteRequest("/system/basic/pos/"+ids).then(resp => {
                        if (resp) {
                            this.innitPositions();
                        }
                    });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            //初始化查询所有数据
            innitPositions() {
                this.getRequest("/system/basic/pos/").then(resp => {
                    if (resp) {
                        this.positions = resp;
                    }
                });
            },
            //单条编辑
            showEditView(index,data) {
                //this.updatePos = data;
                //解决编辑时输入框修改了内容，随之取消修改，页面上显示修改过的内容。
                Object.assign(this.updatePos,data);
                this.dialogVisible = true;
            },
            doUpdate() {
                this.putRequest("/system/basic/pos/",this.updatePos).then(resp => {
                    if (resp) {
                        this.innitPositions();
                        this.dialogVisible = false;
                        this.updatePos.name = '';
                    }
                });
            },
            //单条删除
            handleDelete(index,data) {
                this.$confirm('此操作将永久删除该"'+data.name+'"职位, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.deleteRequest("/system/basic/pos/" + data.id).then(resp => {
                        if (resp) {
                            this.innitPositions();
                        }
                    });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            //添加
            addPosition() {
                //首先判断是否是空的
                if (this.pos.name) {
                    this.postRequest("/system/basic/pos/", this.pos).then(resp => {
                        if (resp) {
                            // this.$message({
                            //     message: '职位添加成功',
                            //     type: 'success'
                            // });
                            this.innitPositions();
                            this.pos.name = '';
                        }
                    });
                } else {
                    this.$message.error("职位名称不能为空")
                }
            }
        }
    }
</script>

<style scoped>
    .addPosIntput {
        width: 300px;
        margin-right: 10px
    }
    .posManaTable {
        margin-top: 10px;
    }
    .updatePosInput {
        width: 200px;
        margin-left: 8px;
    }
</style>
